﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Churchtoolkit.Utils.Authentication;
using Churchtoolkit.Utils.MVP;
using Churchtoolkit.Web.Shell.Views;
using Churchtoolkit.DataAccess.Repositories;
using Churchtoolkit.DataAccess.Entities;
using Churchtoolkit.DataAccess.Common;

namespace Churchtoolkit.Web.Shell.Presenters
{
    public class ModulesPresenter : Presenter<IModulesView>
    {
        #region [ fields ]
        private MemberModulesDtoRepository _memberModules;
        private ModulesRepository _modules;
        private MembersInModulesRepository _memberInModules;
        private IAuthentication _authentication;
        //------------------------------------------------------
        #endregion

        #region [ constructor ]
        public ModulesPresenter(MemberModulesDtoRepository memberModulesRepository, ModulesRepository modules, MembersInModulesRepository membersInModulesRepository, IAuthentication authentication)
        {
            _memberModules = memberModulesRepository;
            _modules = modules;
            _memberInModules = membersInModulesRepository;
            _authentication = authentication;
        }
        #endregion 

        #region [ methods ]
        public IEnumerable<MemberModulesDto> GetCurrentMemberModules()
        {
            return _memberModules.Find(items => items.MemberId = _authentication.CurrentLogedInMember.Id);
        }
        public IEnumerable<Modules> SearchforModules(string txtModule)
        {
            EntityResult<Modules> foundModules = _modules.Find(items => items.ModuleName = string.IsNullOrEmpty(txtModule) ? "%" : txtModule + "%");
            return foundModules.Where(item => !GetCurrentMemberModules().Select(m => m.ModuleId).Contains(item.ModuleId));
        }
        public void RemoveModule(int moduleID)
        {
            _memberInModules.Delete(new MembersInModulesIdentity { MemberId = this._authentication.CurrentLogedInMember.Id, ModuleId = moduleID });
        }
        public void AddModule(int moduleID)
        {
            _memberInModules.Insert(new MembersInModules() { MemberId = this._authentication.CurrentLogedInMember.Id, ModuleId = moduleID });
        }
        #endregion
    }
}
